import re

from django import forms
from django.db import transaction

from accounts.models import User, Profile


class LoginForm(forms.Form):
    """登录表单"""
    username = forms.CharField(label='用户名', max_length=100, initial='admin')
    password = forms.CharField(label='密码', max_length=200, min_length=6,
                               widget=forms.PasswordInput)

    # 验证username 钩子函数
    def clean_username(self):
        username = self.cleaned_data['username']
        pattern = r'^1[0-9]{10}$'
        if not re.search(pattern, username):
            raise forms.ValidationError('手机号码%s输入不正确',
                                        code='invalid_phone',
                                        params=(username,))
        return username

    def clean(self):
        data = super().clean()
        # 单个字段有错误
        if self.errors:
            return
        # username = data['username'] 不满足验证会触发异常导致500
        # password = data['password']
        username = data.get('username', None)
        password = data.get('password', None)
        if username and password:
            # 用户名和密码匹配
            user_list = User.objects.filter(username=username)
            err_list = []
            # 用户存在
            if user_list.count() == 0:
                # raise forms.ValidationError('用户不存在')
                err_list.append(forms.ValidationError('用户不存在'))
            # 密码正确 加密算法
            if not user_list.filter(password=password).exists():
                # raise forms.ValidationError('密码不正确')
                err_list.append(forms.ValidationError('密码不正确'))
            if err_list:
                raise forms.ValidationError(err_list)
        return data


class UserEditForm(forms.Form):
    """用户信息维护"""
    SEX_CHOICES = (
        (1, '男生'),
        (0, '女生')
    )
    username = forms.CharField(label='用户名', max_length=100, initial='admin')
    email = forms.EmailField(label='电子邮箱', max_length=200)
    age = forms.IntegerField(label='年龄')
    sex = forms.ChoiceField(label='性别', choices=SEX_CHOICES,
                            widget=forms.RadioSelect)
    birth_date = forms.DateField(label='生日')
    avatar = forms.ImageField(label='用户头像')


class UserRegForm(forms.Form):
    """"用户注册表单"""
    username = forms.EmailField(label='用户名', max_length=200, min_length=5)
    password = forms.CharField(label='密码', max_length=200, min_length=6,
                               widget=forms.PasswordInput)
    nickname = forms.CharField(label='用户昵称', max_length=32, required=False)
    birth_date = forms.DateField(label='用户生日', required=False)


class UserChangeForm(forms.ModelForm):
    """"从模型创建表单-- 用户信息修改"""
    class Meta:
        model = User
        # fields = ('username', 'password', 'nickname', 'avatar')
        exclude = ('status',)
        labels = {
            'username': '手机号码'
        }
        widgets = {
            'password': forms.PasswordInput(attrs={
                'style': 'border: 1px solid #f00'
            })
        }
        error_messages = {
            'username':{
                'max_length': '用户名超出最大长度限制'
            }
        }

        # 验证username 钩子函数
    def clean_username(self):
        username = self.cleaned_data['username']
        pattern = r'^1[0-9]{10}$'
        if not re.search(pattern, username):
            raise forms.ValidationError('手机号码%s输入不正确',
                                        code='invalid_phone',
                                        params=(username,))
        return username

    @transaction.atomic
    def save(self, commit=False):
        # commit false 不提交数据库
        user_obj = super().save(commit)
        # 修改业务逻辑处理
        user_obj.save()
        # 对其他模型操作
        Profile.objects.create(user=user_obj, username=user_obj.username)
        return user_obj


class AvatarUploadForm(forms.Form):
    """用户头像上传"""
    avatar = forms.ImageField(label='用户头像')